<?php
namespace app\index\controller;

use app\admin\model\Goods;
use app\admin\model\Qutbound;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> YznCMS V' . config('version.yzncms_version') . '<br/><span style="font-size:30px">你值得信赖的PHP框架</span></p></div>';
    }


    /**
     * 导出
     * @return void
     * @throws Throwable
     */
    public function export(): void
    {
        $list =(new Qutbound())
            ->select()
            ->toArray();

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue([1, 1], 'ID');
        $sheet->setCellValue([2, 1], '商品编码');
        $sheet->setCellValue([3, 1], '商品名称');
        $sheet->setCellValue([4, 1], '成本单价');
        $sheet->setCellValue([5, 1], '成本总价');
        $sheet->setCellValue([6, 1], '出库单价');
        $sheet->setCellValue([7, 1], '出库总价');
        $sheet->setCellValue([8, 1], '利润');
        $sheet->setCellValue([9, 1], '创建时间');


        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(20);
        $sheet->getColumnDimension('D')->setWidth(20);
        $sheet->getColumnDimension('E')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);
        $sheet->getColumnDimension('G')->setWidth(20);
        $sheet->getColumnDimension('H')->setWidth(20);
        $sheet->getColumnDimension('I')->setWidth(20);


        $h = 2;
        foreach ($list as $v) {

            $sheet->setCellValue([1, $h], $v['id']);
            $sheet->setCellValue([2, $h], (string)$v['number']);
            $sheet->setCellValue([3, $h], $v['goods_name']);
            $sheet->setCellValue([4, $h], $v['cost_price']);
            $sheet->setCellValue([5, $h], $v['cost_price_total']);
            $sheet->setCellValue([6, $h], $v['qutbound_price']);
            $sheet->setCellValue([7, $h], $v['qutbound_price_total']);
            $sheet->setCellValue([8, $h], $v['income']);
            $sheet->setCellValue([9, $h], $v['create_time']);
            $h++;
        }

        $writer = new Xlsx($spreadsheet);
        $file =  '出库管理.xlsx';
        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Access-Control-Expose-Headers:Content-Disposition');
        header('Content-Disposition: attachment;filename=' . $file);
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
        $spreadsheet->disconnectWorksheets();
        echo "导出成功";
    }



    public function goods(): void
    {
        $list =(new Goods())
            ->select()
            ->toArray();

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue([1, 1], 'ID');
        $sheet->setCellValue([2, 1], '商品编码');
        $sheet->setCellValue([3, 1], '商品名称');
        $sheet->setCellValue([4, 1], '总库存');
        $sheet->setCellValue([5, 1], '进货单价');
        $sheet->setCellValue([6, 1], '出货单价');
        $sheet->setCellValue([7, 1], '创建时间');


        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(20);
        $sheet->getColumnDimension('D')->setWidth(20);
        $sheet->getColumnDimension('E')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);
        $sheet->getColumnDimension('G')->setWidth(20);



        $h = 2;
        foreach ($list as $v) {
            $sheet->setCellValue([1, $h], $v['id']);
            $sheet->setCellValue([2, $h], (string)$v['number']);
            $sheet->setCellValue([3, $h], $v['goods_name']);
            $sheet->setCellValue([4, $h], $v['stock']);
            $sheet->setCellValue([5, $h], $v['stock_price']);
            $sheet->setCellValue([6, $h], $v['qutbound_price']);
            $sheet->setCellValue([7, $h], $v['create_time']);
            $h++;
        }

        $writer = new Xlsx($spreadsheet);
        $file = '商品管理.xlsx';
        ob_end_clean();
        header('Content-Type: application/vnd.ms-excel');
        header('Access-Control-Expose-Headers:Content-Disposition');
        header('Content-Disposition: attachment;filename=' . $file);
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
        $spreadsheet->disconnectWorksheets();
        echo "导出成功";
    }

}
